#include <libl.h>
#include "rtklib.h"
#include "data.h"


//static rtksvr_t obj_rtksvr __attribute__((section("IRAM2")));
static rtksvr_t obj_rtksvr;


#include <dbg/Retarget.c>

int sendchar(int ch)
{
	
	mcu_UartSendChar(0, ch);
	return ch;
}

int bdweek = 0;



/* application defined functions ---------------------------------------------*/
int readeph(int sat, eph_t *eph)
{
	
//	return para_Read(sat, eph, sizeof(eph_t));
	return 0;
}

int writeeph(int sat, const eph_t *eph)
{
	
//	para_Write(sat, eph, sizeof(eph_t));
	return 1;
}

#define TIMET0		1713117038
int gettimeofday(struct timeval *tv, void *ignore)
{
	time_t t = rtc_GetTimet();

	tv->tv_sec = t < TIMET0 ? TIMET0 : t;
	tv->tv_usec = 0;

	return 0;
}

void setbdweek(int week)
{
	
	bdweek = week;
}

void setbdsec(int sec)
{
	gtime_t tb, ts;
	
	if (bdweek)
	{
		tb = bdt2time(bdweek, sec);
		ts = timeget();
		if (timediff(tb, ts) > 8.0f)
			rtc_SetTimet(gpst2utc(tb).time);
	}
}

#if DQUE_BUFFER_QTY==0
os_thd_declare(RTKR, 8000);
void tsk_RTKR(void *args)
{
	
	rtksvrrecv(&obj_rtksvr);
}
#endif

void tsk_RTKT(void *args)
{
	rtksvr_t *p = &obj_rtksvr;
	prcopt_t prcopt = prcopt_default;
	
	p->stream[0] = uart_Open(1, OS_TMO_FOREVER);
	uart_Config(p->stream[0], 115200, UART_PARI_NO, UART_DATA_8D, UART_STOP_1D);
	p->stream[1] = uart_Open(2, OS_TMO_FOREVER);
	uart_Config(p->stream[1], 115200, UART_PARI_NO, UART_DATA_8D, UART_STOP_1D);

	prcopt.mode = PMODE_MOVEB;
	prcopt.nf = 1;
	prcopt.navsys = SYS_ALL;

	tracelevel(1);
	trace(0, "rtkl demo v1.0\n");
#if DQUE_BUFFER_QTY==0
	os_thd_init(RTKR, OS_THDPRI_NORMAL);
#endif
	rtksvrstart(p, &prcopt);
}

